package vno.fux.ui
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.events.Event;
	
	import vno.fux.CompassDirection;
	import vno.fux.GlobalEventScheduler;
	import vno.fux.res.pool.BitmapAttacher;
	import vno.fux.res.pool.ResourcePool;
	import vno.fux.res.pool.WalkTargetResource;
	import vno.fux.utils.BaseAnimation;
	import vno.fux.utils.BitmapUtils;


	public class WalkTargetView extends BaseComponentView
	{
		private var _arrow:BaseAnimation;
		private var _cross:BitmapAttacher;
		private var _tempBitmap:BitmapAttacher;
		public function WalkTargetView(model_:BaseComponentModel, controller_:BaseComponentController)
		{
			super(model_, controller_);
			this.mouseEnabled = false;
			_tempBitmap = new BitmapAttacher;
			_tempBitmap.addEventListener(BitmapAttacher.RECIEVE, doAnime);
			(ResourcePool.getInstance().uiRes.getRes(_model.resId) as WalkTargetResource).arrow.attach(_tempBitmap);
			_cross = new BitmapAttacher;
			(ResourcePool.getInstance().uiRes.getRes(_model.resId) as WalkTargetResource).cross.attach(_cross);
			this.x = _model.pos.x;
			this.y = _model.pos.y;
		}

		private function doAnime(evt:Event):void
		{
			_tempBitmap.removeEventListener(BitmapAttacher.RECIEVE, doAnime);
			(ResourcePool.getInstance().uiRes.getRes(_model.resId) as WalkTargetResource).arrow.detach(_tempBitmap);
			var model:WalkTargetModel = this._model as WalkTargetModel;
			var animeMatrix:Vector.<Vector.<BitmapData>> = BitmapUtils.divide(_tempBitmap.bitmapData, 1, 3);
			var tmp:Array = [];
			for(var i:int=0; i<animeMatrix[0].length; i++)
			{
				tmp[i] = new Bitmap(animeMatrix[0][i]);
			}
			var anime:BaseAnimation =new BaseAnimation(tmp);
			anime.loop = true;
			anime.frameRate = 8;
			anime.mouseEnabled = false;
			anime.reverse = true;

			_tempBitmap = null;
			_arrow = anime;
			//this.content = _arrow;
			this.setRegPoint(CompassDirection.CENTER);
			//_arrow.play();
			//this.addEventListener(Event.ENTER_FRAME, update);
		}
		public function move():void
		{
			if(_arrow.isPlaying())
				return;
			this.content = _arrow;
			_arrow.play();
			GlobalEventScheduler.getInstance().addEnterFrameListener(frameUpdate);
		}

		public function frameUpdate(evt:Event):void
		{
			_arrow.update();
		}

		override public function update(evt:Event):void
		{
			this.x = _model.pos.x;
			this.y = _model.pos.y;
//			trace(_model.pos);
		}

		public function stop():void
		{
			_arrow.stop();
			GlobalEventScheduler.getInstance().removeEnterFrameListener(frameUpdate);
		}
		
		public function cross():void
		{
			if(_arrow.isPlaying())
				return;
			_cross.alpha = 1
			this.content = _cross;
			GlobalEventScheduler.getInstance().addEnterFrameListener(flashUpdate);
		}
		
		private function flashUpdate(evemt:Event):void
		{
			var alpha:Number = _cross.alpha;
			alpha -= 0.05;
			if(alpha<=0)
			{
				this.content = null;
				GlobalEventScheduler.getInstance().removeEnterFrameListener(flashUpdate);
			}
			_cross.alpha = alpha;
		}
	}
}